package com.abs.orm.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;

import com.ads.orm.log.Log;

public class FileUtil implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2033748564816435820L;
	private static Log log = Log.getLogger(FileUtil.class);

	public static void create(String file, String content, String encoding) {
		if (FileUtil.canCreateAble(file, content, encoding)) {
			try {
				OutputStream os = new FileOutputStream(file);
				OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
				osw.flush();
				osw.write(content);
				osw.close();
				os.close();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void create(String file, String content) {
		if (FileUtil.canCreateAble(file, content)) {
			try {
				OutputStream os = new FileOutputStream(file);
				OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
				osw.flush();
				osw.write(content);
				osw.close();
				os.close();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private static boolean canCreateAble(String file, String content, String encoding) {
		return canCreateAble(file, content) && !StringUtil.isEmpty(encoding);

	}

	private static boolean canCreateAble(String file, String content) {
		return !StringUtil.isEmpty(file) && !StringUtil.isEmpty(content);

	}

	public static StringBuffer read(String file, String encoding) {
		StringBuffer sb = new StringBuffer();
		if (FileUtil.canCreateAble(file, encoding)) {
			try {
				InputStream is = new FileInputStream(file);
				InputStreamReader isr = new InputStreamReader(is, encoding);
				int index = 0;
				while ((index = isr.read()) != -1) {
					sb.append((char) index);
				}
				isr.close();
				is.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return sb;
	}

	public static StringBuffer read(String file) {
		StringBuffer sb = new StringBuffer();
		if (!StringUtil.isEmpty(file)) {
			try {
				InputStream is = new FileInputStream(file);
				InputStreamReader isr = new InputStreamReader(is, "UTF-8");
				int index = 0;
				while ((index = isr.read()) != -1) {
					sb.append((char) index);
				}
				isr.close();
				is.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return sb;
	}
}
